package cn.itsource.article.service.impl;


import cn.itsource.article.domain.ArticleType;
import cn.itsource.article.mapper.ArticleTypeMapper;
import cn.itsource.article.query.ArticleTypeQuery;
import cn.itsource.article.service.ArticleTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
@CacheConfig(cacheNames = "cache_article_type")
public class ArticleTypeServiceImpl implements ArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 增
     * @param a
     */
    @Override
    @Transactional
    @CacheEvict(key = "'tree_article_type'")
    public void insert(ArticleType a) {
        if (a.getId()==null){
            articleTypeMapper.insert(a);
        }else {
            System.out.println("用户已存在");
        }
    }



    /**
     * 改
     * @param a
     */
    @Override
    @Transactional
    @CacheEvict(key = "'tree_article_type'")
    public void updateByPrimaryKeySelective(ArticleType a) {
        if (a.getId() != null) {
            articleTypeMapper.updateByPrimaryKeySelective(a);
        }
    }


    /**
     * 分页和查询
     * @param ArticleTypeQuery
     * @return
     */
    @Override
    public PageInfo<ArticleType> pageInfo(ArticleTypeQuery ArticleTypeQuery) {
        PageHelper.startPage(ArticleTypeQuery.getCurrentPage(), ArticleTypeQuery.getPageSize());
        List<ArticleType> all = articleTypeMapper.getAll(ArticleTypeQuery);
        return new PageInfo<ArticleType>(all);
    }

    /**
     * 删除所有
     * @param a
     */
    @Override
    @Transactional
    @CacheEvict(key = "'tree_article_type'")
    public void deleteAll(List<ArticleType> a) {
        articleTypeMapper.deleteAll(a);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    @CacheEvict(key = "'tree_article_type'")
    public void delete(Long id) {
        articleTypeMapper.delete(id);
    }




    @Override
    @Cacheable(key = "'tree_article_type'")
    public List<ArticleType> gettree() {
        //查询所有文章类型
        List<ArticleType> all = articleTypeMapper.selectAll();

        ArrayList<ArticleType> tree = new ArrayList<>();

        //拉姆达表达式
        /*Map<Long, ArticleType> collect = all.stream()
                .collect(Collectors.toMap(ArticleType::getId, Function.identity()));*/
        Map<Long, ArticleType> collect = all.stream().collect(Collectors.toMap(new Function<ArticleType, Long>() {
            @Override
            public Long apply(ArticleType articleType) {
                return articleType.getId();
            }
        }, new Function<ArticleType, ArticleType>() {
            @Override
            public ArticleType apply(ArticleType articleType) {
                return articleType;
            }
        }));
        //循环文章类型
        for (ArticleType articleType : all) {
            //如果父级为空，就是一级
            if (articleType.getParentId() == null) {
                tree.add(articleType);
            } else {
                //拿到父级类型对象
                ArticleType articleType1 = collect.get(articleType.getParentId());
                //装进去
                articleType1.getChildren().add(articleType);
            }
        }
        //放入集合
        return tree;
    }

    @Override
    public List<ArticleType> drawPieChart() {
        List<ArticleType> articleTypes = articleTypeMapper.drawPieChart();
        return articleTypes;
    }


    /**
     * 没有使用SpringCache 用法
     * @return
     */
//    @Override
//    public List<ArticleType> gettree() {
//        List<ArticleType> treeArticle = (List<ArticleType>)redisTemplate.opsForValue().get("tree_article_type");
//        if (treeArticle == null){
//
//            List<ArticleType> all = articleTypeMapper.selectAll();
//
//            //把list集合转为map
//        /*HashMap<Long, ArticleType> map = new HashMap<>();
//        for (ArticleType articleType : all) {
//            map.put(articleType.getId(),articleType);
//        }*/
//
//            ArrayList<ArticleType> tree = new ArrayList<>();
//
//            Map<Long, ArticleType> collect = all.stream().collect(Collectors.toMap(new Function<ArticleType, Long>() {
//                @Override
//                public Long apply(ArticleType articleType) {
//                    return articleType.getId();
//                }
//            }, new Function<ArticleType, ArticleType>() {
//
//                @Override
//                public ArticleType apply(ArticleType articleType) {
//                    return articleType;
//                }
//            }));
//            //循环文章类型
//            for (ArticleType articleType : all) {
//                //如果父级为空，就是一级
//                if (articleType.getParentId()==null){
//                    tree.add(articleType);
//                }else {
//                    //拿到父级类型对象
//                    ArticleType articleType1 = collect.get(articleType.getParentId());
//                    //装进去
//                    articleType1.getChildren().add(articleType);
//                }
//            }
//            //放入缓存
//            redisTemplate.opsForValue().set("tree_article_type",tree);
//            return tree;
//        }
//        return treeArticle;
//    }


}
